package org.koyi.fragments;

import java.util.ArrayList;
import java.util.List;

import org.koyi.birrapp.DetallesActivity;
import org.koyi.database.FuncionesBaseDatos;
import org.koyi.entities.Bares;
import org.koyi.entities.BaresCervezasTapasVasos;
import org.koyi.listadapters.FavoritosAdapterListItem;
import org.koyi.singleton.BaresSingleton;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;

public class FavoritosListFragment extends ListFragment {

	private List<Bares> listaBares = null;
	private List<Bares> listaBaresAux = null;
	
	private List<String> listaNombre = null;
	
	private FuncionesBaseDatos funcionesBaseDatos = new FuncionesBaseDatos();
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) 
	{
        super.onActivityCreated(savedInstanceState);
        
        initializeList();
        
        getActivity().setTitle("Locales Favoritos");
        
        setListAdapter(new FavoritosAdapterListItem(getActivity(), listaBares, this));
    }
	
	public void updateList(int position)
	{
		listaBares.remove(position);
		((FavoritosAdapterListItem)this.getListAdapter()).notifyDataSetChanged();
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) 
	{
		super.onListItemClick(l, v, position, id);
		
		Intent intent = new Intent(this.getActivity(), DetallesActivity.class);
		intent.putExtra("selectedBar", (BaresCervezasTapasVasos)l.getItemAtPosition(position));
		startActivity(intent);		
	}

	private void initializeList ()
	{
		listaBares = new ArrayList<Bares>();
		listaBaresAux = new ArrayList<Bares>();
		listaNombre = new ArrayList<String>();
		
		SQLiteDatabase birrAppDataBase = funcionesBaseDatos.conectaWritableBaseDeDatos(getActivity());
        
        String[] campos = new String[]{"_id", "BAR_NOMBRE"};
        
        if(birrAppDataBase != null){
                
            Cursor cursor = birrAppDataBase.query("BARES", campos, null, null, null, null, null);
            
            while(cursor.moveToNext())
            {
            	listaNombre.add(cursor.getString(1));
            }
            
            funcionesBaseDatos.cierraBaseDeDatos(birrAppDataBase);
        }
        
        if( listaNombre != null && !listaNombre.isEmpty())
        {
        	listaBares.addAll(BaresSingleton.getListaBares());
        }
        
        for(Bares bar : listaBares)
        {
        	if( listaNombre != null && !listaNombre.isEmpty() && !listaNombre.contains(bar.getNombre()))
        	{
        		listaBaresAux.add(bar);
        	}
        }
        
        listaBares.removeAll(listaBaresAux);
	}

	@Override
	public void onResume() {
		super.onResume();
		
		initializeList();
		setListAdapter(new FavoritosAdapterListItem(getActivity(), listaBares, this));
	}
	
	
}
